<template>
  <el-dialog
    :title="!dataForm.skillsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="55%"
    top="8vh"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="技能名称" prop="skillsName">
        <el-input v-model="dataForm.skillsName" placeholder="技能名称" />
      </el-form-item>
      <el-form-item label="技能图片" prop="skillsImg">
        <el-upload
          ref="my-upload"
          class="upload-demo"
          action="http://localhost:8765/admin/oss/file/upload?module=personalskills"
          :on-success="handleAvatarSuccess"
          :on-error="handleAvatarError"
          :on-exceed="handleUploadExceed"
          :before-upload="beforeAvatarUpload"
          :file-list="fileList"
          list-type="picture"
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2Mb</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="技能简介" prop="skillsSynopsis">
        <el-input v-model="dataForm.skillsSynopsis" type="textarea" autosize placeholder="技能简介" />
      </el-form-item>
      <el-form-item label="熟练度" prop="proficiency">
        <template>
          <div class="block">
            <el-slider
              v-model="dataForm.proficiency"
              show-input
            />
          </div>
        </template>
      </el-form-item>
      <el-form-item label="入坑时间" prop="startLearn">
        <el-input v-model="dataForm.startLearn" placeholder="入坑时间" />
      </el-form-item>
      <el-form-item label="出师时间" prop="endLearn">
        <el-input v-model="dataForm.endLearn" placeholder="出师时间" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import reqApi from '@/api/personalskills'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        skillsId: '',
        skillsName: '',
        skillsImg: '',
        skillsSynopsis: '',
        proficiency: '',
        startLearn: '',
        endLearn: ''
      },
      dataRule: {
        skillsName: [
          { required: true, message: '技能名称不能为空', trigger: 'blur' }
        ],
        skillsImg: [
          { required: true, message: '技能图片不能为空', trigger: 'blur' }
        ],
        skillsSynopsis: [
          { required: true, message: '技能简介不能为空', trigger: 'blur' }
        ],
        proficiency: [
          { required: true, message: '熟练度不能为空', trigger: 'blur' }
        ],
        startLearn: [
          { required: true, message: '入坑时间不能为空', trigger: 'blur' }
        ],
        endLearn: [
          { required: true, message: '出师时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.skillsId = id || ''
      this.visible = true
      this.dataForm.proficiency = 0
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.skillsId) {
          reqApi.getInfo(this.dataForm.skillsId).then(({ data }) => {
            if (data) {
              this.dataForm.skillsName = data.personalSkills.skillsName
              this.dataForm.skillsImg = data.personalSkills.skillsImg
              this.dataForm.skillsSynopsis = data.personalSkills.skillsSynopsis
              this.dataForm.proficiency = parseInt(data.personalSkills.proficiency)
              this.dataForm.startLearn = data.personalSkills.startLearn
              this.dataForm.endLearn = data.personalSkills.endLearn
            }
          })
        } else {
          this.fileList = []
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.skillsId) {
            reqApi.save(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            reqApi.update(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    },
    // 上传多于一个文件
    handleUploadExceed(files, fileList) {
      this.$message.warning('想要重新上传图片，请先删除已上传的视频')
    },
    // 上传校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    // 上传成功回调
    handleAvatarSuccess(res, file) {
      console.log(res)
      if (res.success) {
        // console.log(res)
        this.dataForm.skillsImg = res.data.url
        // 强制重新渲染
        // this.$forceUpdate()
      } else {
        this.$message.error('上传失败1')
      }
    },

    // 错误处理
    handleAvatarError() {
      this.$message.error('上传失败2')
    }
  }
}
</script>
